﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorrentService;
using Meebey.SmartIrc4net;
using System.IO;
using System.Net;
using XmlSettings;

namespace BotTorrent
{
    internal static class miscFunctions
{
    // Methods
    public static void _ircLogin(object eventargs, ircbot oIrcBot, string[] command)
    {
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        try
        {
            if (command[1] == oIrcBot.Settings.IrcSettings.ircBotPassword)
            {
                oIrcBot.AuthedUser = oEventArgs.Data.Nick + "@" + oEventArgs.Data.Host;
                oIrcBot.Op(oEventArgs.Data.Channel, oEventArgs.Data.Nick);
            }
        }
        catch (Exception)
        {
        }
    }

    public static void CleanupOldTorrents(string foldername)
    {
        if (Directory.Exists(foldername))
        {
            FileInfo[] rgFiles = new DirectoryInfo(foldername).GetFiles("*.torrent");
            foreach (FileInfo fi in rgFiles)
            {
                try
                {
                    File.Delete(fi.FullName);
                }
                catch (Exception)
                {
                }
            }
        }
    }

    public static string DownloadFile(string filetoget, Settings settings)
    {
        string remoteFilename = filetoget;
        string localFilename = Path.GetFileName(remoteFilename);
        string localFullPath = Path.Combine(settings.TorrentSettings.torrentsPath, localFilename);
        int bytesProcessed = 0;
        Stream remoteStream = null;
        Stream localStream = null;
        WebResponse response = null;
        try
        {
            try
            {
                WebRequest request = WebRequest.Create(remoteFilename);
                if (request != null)
                {
                    int bytesRead;
                    response = request.GetResponse();
                    if (response == null)
                    {
                        return localFullPath;
                    }
                    remoteStream = response.GetResponseStream();
                    localStream = File.Create(localFullPath);
                    byte[] buffer = new byte[0x400];
                    do
                    {
                        bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
                        localStream.Write(buffer, 0, bytesRead);
                        bytesProcessed += bytesRead;
                    }
                    while (bytesRead > 0);
                }
                return localFullPath;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        finally
        {
            if (response != null)
            {
                response.Close();
            }
            if (remoteStream != null)
            {
                remoteStream.Close();
            }
            if (localStream != null)
            {
                localStream.Close();
            }
        }
        return localFullPath;
    }

    public static string GetSource(IrcEventArgs oEventArgs)
    {
        if (oEventArgs.Data.Channel != null)
        {
            return oEventArgs.Data.Channel;
        }
        return oEventArgs.Data.Nick;
    }

    internal static void MessageChannel(string message, ircbot IrcBot)
    {
        try
        {
            IrcBot.SendMessage(SendType.Message, IrcBot.Settings.IrcSettings.ircChannel, message);
        }
        catch (Exception)
        {
        }
    }

    public static string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < size; i++)
        {
            char ch = Convert.ToChar(Convert.ToInt32(Math.Floor((double) ((26.0 * random.NextDouble()) + 65.0))));
            builder.Append(ch);
        }
        if (lowerCase)
        {
            return builder.ToString().ToLower();
        }
        return builder.ToString();
    }
}

 

}
